// Editor: myBigPro9/src001/main/java/org/csu/mybigpro/controller/DocumentAnalysisController.java
// Action: Create
package org.csu.mybigpro.controller;

import org.csu.mybigpro.DTO.SolvedQuestionDto;
import org.csu.mybigpro.service.DocumentAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/document")
public class DocumentAnalysisController {

    @Autowired
    private DocumentAnalysisService documentAnalysisService;

    @PostMapping("/solve-questions")
    public ResponseEntity<?> solveQuestionsFromDocument(@RequestParam("document") MultipartFile documentFile) {
        if (documentFile.isEmpty()) {
            return ResponseEntity.badRequest().body(Map.of("error", "上传的Word文档不能为空。"));
        }
        try {
            List<SolvedQuestionDto> results = documentAnalysisService.analyzeDocument(documentFile);
            return ResponseEntity.ok(results);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.internalServerError().body(Map.of("error", "处理失败: " + e.getMessage()));
        }
    }
}